﻿using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;

namespace FreesenseSubAIS.Core
{
    public class Device
    {
        public static DeviceParameter Parameter;
        public static ProjectParam defaultProduct;
        public static bool EnginerModel = false;
        public static string AppPath;
        public static string ProjectPath;
        public static string ExtraPathDir;
        public static bool SaveSourceImage = false;//是否保存原图
        public static bool DisDetail = true;
        public static string SubID;//从机ID
        public static bool ICEconnection = false;
        public static bool Automode = false;
        public static int CorrectIndex = 0;
        public static int InspRecordCount = 0;//检测记录数量
        public static string LogPath;
        internal static BaseDeviceControl DeviceEntityControl;//从机硬件管理
        public static Dictionary<string, bool> DitUseVirtual;
        public static readonly int[] CorrectArray = new[] { 0, 1, 2 };//矫正图标识
        internal static string UniqueID;
        internal static DateTime LoadingTime;

        public static void Init()
        {
            try
            {
                Parameter = new DeviceParameter();
                ReadParameter();
                DitUseVirtual = new Dictionary<string, bool>();
                DeviceEntityControl = new DeviceControl0176(Parameter);
                defaultProduct = ProductManager.OpenProduct(Parameter.DefaultProductId);
                Automode = Properties.Settings.Default.AutoMode;
                Thread.Sleep(50);
            }
            catch (Exception ex)
            {
                BpjbCommon.Net.Core.BpEventHandler.ErrHandler(ex.Message, BpjbCommon.Net.Core.ErrType.Warning);
            }
            Thread.Sleep(80);
        }

        internal static bool AllReady()
        {
            var ready = DeviceEntityControl.BackLightS.GetHashCode() == 1
                && DeviceEntityControl.CameraS.GetHashCode() == 1
                 && DeviceEntityControl.ElectricS.GetHashCode() == 1
                  && DeviceEntityControl.SideDownLightS.GetHashCode() == 1
                   && DeviceEntityControl.SideUpLightS.GetHashCode() == 1;
            return ready;
        }

        public static void SaveParameter()
        {
            BinarySerializeHelper.FormatFile("CONFIG.COG", Parameter);
        }

        public static void ReadParameter()
        {
            if (File.Exists("CONFIG.COG"))
            {
                Parameter = (DeviceParameter)BinarySerializeHelper.DeformatFile("CONFIG.COG");
            }
            else
            {
                Parameter = DeviceParameter.GetDefaultDeviceParameter();
            }
        }
    }
}
